package com.ttai.presenter.activity;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.SRPTwoBean;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.untils.SRPClient;
import com.ttai.untils.SRPUtil;

import java.util.List;

import retrofit2.Call;

import static android.support.constraint.Constraints.TAG;

public class SRPTwoPresenter extends BasePresenter {

//    SRPOnePresenter srpOnePresenter = new SRPOnePresenter();
    SRPThreePresenter srpThreePresenter = new SRPThreePresenter();
    SRPClient srpClient;


    @Override
    protected void showError(String message) {
        System.out.println("请求失败 :"+message);

    }

    @Override
    protected void parseJson(JsonObject json) {
        Gson gson = new Gson();
        SRPTwoBean srpTwoBean = gson.fromJson(json,SRPTwoBean.class);
        String paramB = srpTwoBean.getParamB();
        Log.i(TAG, "parseJson: paramB"+paramB);

        List<String> st = SRPUtil.spiString(EnterPage.equipmentEnvir,":");
        System.out.println("enquip: "+EnterPage.equipmentEnvir);
        String[] pwd = new String[st.size()];
        for (int i=0;i<st.size();i++){
            pwd[i] = SRPUtil.tob64(st.get(i).getBytes());
        }


        srpClient.inputPassword(pwd[2]);
//        cli.inputPassword(pwd[0]+pwd[1]+pwd[2]+pwd[3]+pwd[4]);
        byte[] key = srpClient.getSessionKey(SRPUtil.fromb64(paramB));
        String Ckey = SRPUtil.tohex(key);
        System.out.println("Session key form client: " + Ckey);

        srpThreePresenter.srpThree( SRPUtil.tohex(srpClient.response()),EnterPage.enterphonenumber);


    }
    public void srpTwo(String paramA,String phone){
        Call<ResponseInfo> srptwo = responseInfoApi.srptwo(paramA, phone);
        srptwo.enqueue(new CallBackAdapter());
    }
    public void getClient(SRPClient srpClient){
        this.srpClient = srpClient;
    }
}
